package com.pacy.swj.swjwb.activitys;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.reflect.TypeToken;
import com.gyf.immersionbar.ImmersionBar;
import com.pacy.swj.swjwb.MainActivity;
import com.pacy.swj.swjwb.R;
import com.pacy.swj.swjwb.Utils.Constans;
import com.pacy.swj.swjwb.Utils.DividerDecoration;
import com.pacy.swj.swjwb.Utils.HttpUtils;
import com.pacy.swj.swjwb.adapters.TypeListAdapter;
import com.pacy.swj.swjwb.beans.BaseBean;
import com.pacy.swj.swjwb.beans.QureyMapBean;
import com.pacy.swj.swjwb.views.HRecyclerView;

import org.xutils.common.Callback;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import h.framework.android.utils.JsonUtil;
import h.framework.android.utils.ScreenUtil;
import h.framework.android.utils.ToastUtil;

public class ThreeListActivity extends AppCompatActivity {
    @BindView(R.id.title_tv)
    TextView titleTv;
    @BindView(R.id.right_tv)
    TextView right_tv;

    TypeListAdapter mAdapter;
    GridLayoutManager manager;
    @BindView(R.id.recyclerView)
    HRecyclerView recycler;

    long pid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_type_list);
        ImmersionBar.with(this).statusBarColor(R.color.colorPrimary).init();
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        titleTv.setText(getIntent().getStringExtra("title"));
        right_tv.setText(getIntent().getStringExtra("right"));
        pid = getIntent().getLongExtra("id", 0);
        initRecyclerView();
    }


    /**
     * 初始化列表
     */
    private void initRecyclerView() {
        mAdapter = new TypeListAdapter();
        manager = new GridLayoutManager(ThreeListActivity.this, 2);
        DividerDecoration itemDecoration = new DividerDecoration(Color.TRANSPARENT, ScreenUtil.dp2px(30));
        recycler.addItemDecoration(itemDecoration);
        recycler.setLayoutManager(manager);
        recycler.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(ThreeListActivity.this, FinalListActivity.class);
                intent.putExtra("title",mAdapter.getItem(position).getMenuName());
                intent.putExtra("id",   mAdapter.getItem(position).getId());
                intent.putExtra("right", "首页");
                startActivity(intent);
            }
        });

        sendRequest();
    }




    /**
     * 发送请求
     */
    private void sendRequest() {
        Map<String, Object> map = new HashMap<>();
        map.put("menu_Level", "3");
        map.put("menu_pid", pid);
        HttpUtils.Post(Constans.QUERYBYMAP, map, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.i("SWJ", result);
                BaseBean<List<QureyMapBean>> baseBean = JsonUtil.jsonToObject(result, new TypeToken<BaseBean<List<QureyMapBean>>>() {
                }.getType());
                if (baseBean.isSuccess()) {
                    List<QureyMapBean> list = new ArrayList<>();
                    if (baseBean.getData().size() > 0) {
                        for (QureyMapBean qureyMapBean : baseBean.getData()) {
                            if (qureyMapBean.getMenuStatus() == 1) {
                                list.add(qureyMapBean);
                            }
                        }
                    }
                    mAdapter.setNewData(list);
                } else {
                    ToastUtil.error(baseBean.getMessage());
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.i("SWJ", "请求出错---" + ex.toString());
                ToastUtil.error("请求失败,请重试");
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }


    @OnClick(value = {R.id.back_iv, R.id.right_tv})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_iv:
                finish();
                break;

            case R.id.right_tv:
                Intent intent = new Intent(ThreeListActivity.this, MainActivity.class);
                startActivity(intent);
                break;
        }
    }
}
